<script>
import { Message } from "element-ui";
export default {
  data() {
    return {
      loading: false,
      total: 50,
      param: {
        pageNo: 1,
        pageSize: 5,
        name:''
      },
      tableData: [],
      form:{
        name:''
      },

      title: "添加课程类别",
      dialogFormVisible: false,
    };
  },
  methods: {
    // 清除数据
    clear() {
      this.dialogFormVisible = false;
      this.form = {
        name:""
      }
    },

    //获取表格数据
    get() {
      this.loading = true;
      this.$http
        .get("/courseType/list", {
          params: this.param,
        })
        .then((res) => {
          console.log(res.data);
          if (res.data.status == 200) {
            this.tableData = res.data.data.list;
            this.total = res.data.data.total;
            this.loading = false;
          } else {
            Message({
              type: "error",
              message: "获取数据失败",
            });
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },

    // 新增的表单
    addForm() {
      this.title = "添加课程类别";
      this.dialogFormVisible = true;
    },
    // 添加
    add() {
      // let user = JSON.parse(sessionStorage.getItem("user"));
      // this.courseForm.teacherId = user.id;

      this.$http
        .post("/courseType/add",this.form)
        .then((res) => {
          console.log(res.data);
          if (res.data.status == 200) {
            this.get();
            this.clear();
          } else {
          }
        })
        .catch((e) => {
          this.$message.error(e)
        });
    },

    editForm(item) {
      this.title = "修改课程类别";
      this.dialogFormVisible = true;
      this.form = item;
      // this.dialogVisible = true
      if(item.courseVidio != null){
        this.fileList=[{
        name:item.courseVidio,
        url:this.baseURL+item.courseVidio
      }]
      }
    },
    edit() {
      this.$http
        .post("/courseType/update", this.form)
        .then((res) => {
          console.log(res.data);
          if (res.data.status == 200) {
            this.get()
            this.clear()
          } else {
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },

    // 删除
    remove(item) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$http
          .post("/courseType/delete?id="+item)
          .then((res) => {
            console.log(res.data);
            if (res.data.status == 200) {
              this.$message.success("");
              this.get();
            }
          })
          .catch((e) => {
            console.log(e);
          });
      }).catch(()=>{
        this.$message({
            type: 'info',
            message: '已取消删除'
          })
      })
    },

    //分页
    handleSizeChange(val) {
      this.param.pageSize = val;
      this.get();
    },
    handleCurrentChange(val) {
      this.param.pageNo = val;
      this.get();
    },
  },
  watch: {
  },
  mounted() {
    this.get();
  },
};
</script>
<template>
  <div>
    <!-- 搜索栏 -->
    <div class="head">
      <el-row>
        <span>课程分类</span>
        <el-input
          clearable
          @clear="get"
          style="width: 200px; margin-right: 10px"
          v-model="param.name"
          placeholder="请输入课程类型名称"
        ></el-input>
        <el-button type="primary" @click="get">查找</el-button>
        <el-button type="primary" @click="addForm">新增</el-button>
        <!-- <el-button type="primary">管理</el-button> -->
      </el-row>

      <!-- 新增表单 -->
      <el-dialog
        :title="title"
        :visible.sync="dialogFormVisible"
        @close="clear"
      >
        <el-form :model="form">
          <el-form-item>
            <span>课程类别</span>
            <el-input
              v-model="form.name"
              style="width: 200px"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="clear">取 消</el-button>
          <el-button type="primary" @click="add" v-show="title == '添加课程类别'"
            >添加</el-button
          >
          <el-button type="primary" @click="edit" v-show="title == '修改课程类别'"
            >保 存</el-button
          >
        </div>
      </el-dialog>
    </div>

    <!-- 列表 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      height="530"
      v-loading="loading"
      :header-cell-style="{ background: '#1890FF', color: 'white' }"
    >
      <el-table-column fixed prop="id" label="课程类型id" width="200">
      </el-table-column>
      <el-table-column fixed prop="name" label="课程类型" width="120">
      </el-table-column>
      <el-table-column fixed prop="courseMount" label="课程数量" width="120">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click="editForm(scope.row)"
            >编辑</el-button
          >
          <el-button type="danger" @click="remove(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="param.pageNo"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="param.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-top: 10px;"
    >
    </el-pagination>
  </div>
</template>
<style>
span {
  margin: 2px 10px;
}
.head {
  margin: 10px 5px;
}
.el-button {
  margin: 5px 20px;
}
</style>